﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ProCodes.BugTracker.BugCatcher.Emails
{
    public partial class SendEmails : PRO_DOCS.BLL.WIN.Templates.DevxPermissionTemplate
    {
        public SendEmails()
        {
            InitializeComponent();
        }

        public enum EmailType
        {
            /// <summary>
            /// 
            /// </summary>
            Email,
            /// <summary>
            /// 
            /// </summary>
            Bug,
            /// <summary>
            /// 
            /// </summary>
            Feature
        }

        private EmailType sendingEmailType;

        /// <summary>
        /// Gets or sets the type of the sending email.
        /// </summary>
        /// <value>The type of the sending email.</value>
        public EmailType SendingEmailType
        {
            get { return sendingEmailType; }
            set { sendingEmailType = value; }
        }

        public string ToEmail
        {
            get { return txtEmailTo.Text; }
            set { txtEmailTo.Text = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        private string memberName;

        /// <summary>
        /// Gets or sets the name of the member.
        /// </summary>
        /// <value>The name of the member.</value>
        public string MemberName
        {
            get { return memberName; }
            set { memberName = value; }
        }

        public string memberType;
        /// <summary>
        /// Gets or sets the type of the member.
        /// </summary>
        /// <value>The type of the member.</value>
        public string MemberType
        {
            get { return memberType; }
            set { memberType = value; }
        }

        /// <summary>
        /// Gets or sets the email body.
        /// </summary>
        /// <value>The email body.</value>
        public string EmailBody
        {
            set { txtEmailBody.Text = value; }
            get { return txtEmailBody.Text; }
        }

        /// <summary>
        /// Sets the initial feature email body.
        /// </summary>
        private void SetInitialFeatureEmailBody()
        {
            EmailBody = Properties.Settings.Default.FeatureEmailBody
                .Replace("{ExceptionMessage}", String.Empty)
                .Replace("{receiver}", "Ahmed Mohammed Fahmy")
                .Replace("{nl}", Environment.NewLine)
                .Replace("{Source}", String.Empty)
                .Replace("{employeeName}", PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.EMPLOYEE_FULL_NAME)
                .Replace("{emailAdress}", PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.EMPLOYEE_EMAIL)
                .Replace("{employeeMobile}", PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.EMPLOYEE_EMAIL);
        }

        /// <summary>
        /// Handles the Load event of the SendEmails control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void SendEmails_Load(object sender, EventArgs e)
        {

            AddEmailAttachments();
            switch (SendingEmailType)
            {
                case EmailType.Bug:
                    txtSubject.Text = Properties.Settings.Default.BugEmailSubject.Replace("{CompanyName}", "blue o two")
                                                                                 .Replace("{AppName}", "PRO®- blue o two POS.");
                    break;
                case EmailType.Feature:
                    txtSubject.Text = Properties.Settings.Default.FeatureEmailSubject.Replace("{CompanyName}", "blue o two")
                                                                                 .Replace("{AppName}", "PRO®- blue o two POS.");
                    SetInitialFeatureEmailBody();
                    break;
                default:
                    break;
            }


        }

        /// <summary>
        /// Handles the Click event of the btnSendEmail control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSendEmail_Click(object sender, EventArgs e)
        {
            string[] emailTo = txtEmailTo.Text.Split(new Char[] { ';', ',', ':' });
            string[] emailCc = txtEmailCc.Text.Split(new Char[] { ';', ',', ':' });
            string[] emailBCc = txtEmailBcc.Text.Split(new Char[] { ';', ',', ':' });
            MailOption.SendEmail(txtSubject.Text,
                                     EmailBody,
                                     emailTo,
                                     emailCc,
                                     emailBCc,
                                     Emails.EmailAttachments.AttachedFiles.ToArray());
        }

        /// <summary>
        /// Handles the Click event of the btnAttachments control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnAttachments_Click(object sender, EventArgs e)
        {
            txtAttachments.Text = String.Empty;

            new Emails.EmailAttachments().ShowDialog();

            AddEmailAttachments();
        }

        /// <summary>
        /// Adds the email attachments.
        /// </summary>
        private void AddEmailAttachments()
        {
            for (int i = 0; i < Emails.EmailAttachments.AttachedFiles.Count; i++)
            {
                string commaSeparator = (Emails.EmailAttachments.AttachedFiles.Count <= (i + 1)) ? String.Empty : ", ";
                txtAttachments.Text += new FileInfo(Emails.EmailAttachments.AttachedFiles[i]).Name + commaSeparator;
            }

        }

        /// <summary>
        /// Handles the FormClosing event of the SendEmails control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void SendEmails_FormClosing(object sender, FormClosingEventArgs e)
        {
            Emails.EmailAttachments.AttachedFiles.Clear();
            Emails.EmailAttachments.AttachmentsSize = 0;
        }
    }
}
